import Collect from "../models/collect.js";
import Concern from "../models/concern.js";
import Home from "../models/homePage.js";
import { successResponse, errorResponse } from "../utils/responseHelper.js";
import User from "../models/user.js";

export const getHomes = async (ctx) => {
  const username = ctx.params.username;
  try {
    const count = await Collect.count({
      where: {
        user: username,
      },
    });
    successResponse(ctx, count);
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};
export const getAllCollects = async (ctx) => {
  try {
    // 查询 Collect 表中指定用户的所有记录
    const collects = await Collect.findAll();

    // 使用 Promise.all 来并行查询 Concern 和 Home 表
    const results = await Promise.all(
      collects.map(async (collect) => {
        const concernData = await Concern.findOne({
          where: { user: collect.user },
        });

        const homeData = await Home.findOne({
          where: { id: collect.user_con },
        });

        // 返回合并后的数据
        return {
          collectId: collect.get().id,
          ...collect.get(),
          ...concernData.get(),
          ...homeData.get(),
        };
      })
    );

    // 返回结合后的数据
    successResponse(ctx, results);
  } catch (error) {
    errorResponse(ctx, 500, "获取数据时出错");
  }
};
export const getHomeById = async (ctx) => {
  const { user } = ctx.params;

  try {
    const home = await Collect.findAll({ where: { user: user } });
    if (home) {
      successResponse(ctx, home);
    } else {
      successResponse(ctx, "Not found");
    }
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};
export const getCancel = async (ctx) => {
  const { user } = ctx.params;
  try {
    const deletedCount = await Collect.destroy({
      where: {
        id: user,
      },
    });
    successResponse(ctx, deletedCount);
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};

export const concern = async (ctx) => {
  const { id, username } = ctx.request.body;
  console.log(id, username);
  try {
    const homeData = await Home.findOne({ where: { id: id } });
    if (!homeData) {
      console.log("没这个直播");
      errorResponse(ctx, 500, "没这个直播");
      return;
    }
    const userData = await User.findOne({ where: { username: username } });
    if (!userData) {
      console.log("没有这个用户");
      errorResponse(ctx, 500, "没有这个用户");
      return;
    }

    console.log("========================");
    const newCollect = await Collect.create({
      user_con: id,
      user: username,
    });
    successResponse(ctx, newCollect);
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};

export const collect = async (ctx) => {
  const { id, username } = ctx.request.body;
  console.log(id, username);
  try {
    const homeData = await Home.findOne({ where: { id: id } });
    if (!homeData) {
      console.log("没这个直播");
      errorResponse(ctx, 500, "没这个直播");
      return;
    }
    const userData = await User.findOne({ where: { username: username } });
    if (!userData) {
      console.log("没有这个用户");
      errorResponse(ctx, 500, "没有这个用户");
      return;
    }

    console.log("========================");
    const newCollect = await Collect.create({
      user_con: id,
      user: username,
    });
    successResponse(ctx, newCollect);
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};

export const upDateHome = async (ctx) => {
  const { collectId, username, id } = ctx.request.body;
  // 查找指定 id 的记录
  console.log(collectId, username, id);
  const collect = await Collect.findOne({ where: { id: collectId } });
  const userData = await User.findOne({ where: { username: username } });
  if (!userData) {
    console.log("没有这个用户");
    errorResponse(ctx, 500, "没有这个用户");
    return;
  }
  const homeData = await Home.findOne({ where: { id: id } });
  if (!homeData) {
    console.log("没这个直播");
    errorResponse(ctx, 500, "没这个直播");
    return;
  }
  if (!collect) {
    return errorResponse(ctx, 404, "记录未找到");
  }

  // 更新记录
  await collect.update({ user: username, user_con: id });

  // 返回更新后的记录
  successResponse(ctx, collect);
};
